'''
创立了一个blueprint，前缀是/request，后续就是引入刚才的request类，进行http请求，最后返回response。最后回到run.py 注册蓝图
'''
from flask import Blueprint, request, jsonify
from app.middlerware.HttpClient import Request

# 创建发送请求蓝图
from app.utils.decorator import permission

req = Blueprint("request", __name__, url_prefix="/request")


@req.route("/http", methods=["POST"])
@permission
# @permission(lxq.config.get("ADMIN"))
def http_request(user_info):
    # 拿到所有请求的数据
    data = request.get_json()

    method = data.get("method","GET")
    if not method:
        return jsonify(dict(code=101, msg="请求方法不能为空"))

    url = data.get("url")
    if not url:
        return jsonify(dict(code=101, msg="请求地址不能为空"))

    body = data.get("body")
    headers = data.get("headers")
    # 调用封装的request
    response = Request(url, data=body, headers=headers).request(method)

    # 判断里面的status是否为True，不为True的话返回110状态码
    if response.get("status"):
        return jsonify(dict(code=0, data=response,msg="操作成功"))
    return jsonify(dict(code=110,data=response, msg=response.get("msg")))

